package cn.wei.reallyEnjoy.user.adapter;

import static android.content.ContentValues.TAG;
import static android.content.Context.MODE_PRIVATE;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;

import com.bumptech.glide.Glide;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.List;

import cn.wei.reallyEnjoy.R;
import cn.wei.reallyEnjoy.app.LoginActivity;
import cn.wei.reallyEnjoy.app.RecordDetailActivity;
import cn.wei.reallyEnjoy.home.bean.DiaryBean;
import cn.wei.reallyEnjoy.home.bean.RecordBean;
import cn.wei.reallyEnjoy.user.activity.MyDiaryActivity;
import cn.wei.reallyEnjoy.utils.Constants;
import okhttp3.Call;

public class MyDiaryAdapter extends BaseAdapter {
    private List<DiaryBean> data;
    private Context context;
    public MyDiaryAdapter(List<DiaryBean> data, Context context) {
        this.data = data;
        this.context = context;
    }



    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * 返回每一个条目，并设值
     * @param position
     * @param convertView
     * @param parent
     * @return
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView==null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.my_diary_grid_view, parent, false);
        }
        ImageView iv_img = convertView.findViewById(R.id.iv_img);
        TextView tv_title = convertView.findViewById(R.id.tv_title);

        Button btn_deleteMyDiary = convertView.findViewById(R.id.btn_deleteMyDiary);

        TextView tv_favorites = convertView.findViewById(R.id.tv_favorites);
        //根基位置得到对应的数据
        DiaryBean diaryBean = data.get(position);
        //Todo
        Glide.with(context).load(Constants.IMG_URL+diaryBean.getContentImg()).into(iv_img);
        tv_title.setText(diaryBean.getTitle());
        tv_favorites.setText(diaryBean.getPrefer());
        //
        iv_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //日志记录对象
                //TODO
                RecordBean recordBean = new RecordBean();
                recordBean.setRecord_id(diaryBean.getId());
                recordBean.setImg(diaryBean.getContentImg());
                recordBean.setContent(diaryBean.getContent());
                recordBean.setTitle(diaryBean.getTitle());
                recordBean.setUname(diaryBean.getUname());
                recordBean.setUheadImg(diaryBean.getUheadImg());
                recordBean.setPrefer(diaryBean.getPrefer());
                recordBean.setCreateTime(diaryBean.getCreateTime());
                startRecordDetailActivity(recordBean);
            }
        });

        btn_deleteMyDiary.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(context)
                        .setTitle("提示")
                        .setMessage("是否删除")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                String uid = getUid();
                                String diaryId = diaryBean.getId();
                                String url = Constants.DELETE_MY_DIARY_URL+uid+"/"+diaryId;

                                OkHttpUtils.delete()
                                        .url(url)
                                        .build()
                                        .execute(new StringCallback() {
                                            @Override
                                            public void onError(Call call, Exception e, int id) {
                                                Toast.makeText(context, "网络错误", Toast.LENGTH_SHORT).show();
                                            }

                                            @Override
                                            public void onResponse(String response, int id) {
                                                Toast.makeText(context, "删除成功", Toast.LENGTH_SHORT).show();
                                                data.remove(position);
                                                notifyDataSetChanged();
                                            }
                                        });
//
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        })
                        .show();


            }
        });
        return convertView;
    }
    private String getUid(){
        SharedPreferences sp = context.getSharedPreferences("user", MODE_PRIVATE);
        return  sp.getString("id", "");
    }
    private void startRecordDetailActivity(RecordBean recordBean) {
        Intent intent = new Intent(context, RecordDetailActivity.class);
        intent.putExtra("recordBean",recordBean);
        context.startActivity(intent);
    }
}
